
clear all

* Set $root to current directory *
global root "FIXME" 
global output "$root/Tables/"

use "$root/Data/analysis_2.dta"

cd "$output"

lab var J "\hspace{0.2cm} J"
lab var treat "\hspace{0.2cm} $\widehat{\text{Preferred}}$"
lab var s_treat_d "\hspace{0.2cm} $\widehat{\text{Share other preferred}}_{d}^{-i}$"
lab var s_treat_r "\hspace{0.2cm} $\widehat{\text{Share other preferred}}_{r}^{-i}$"
lab var z_adc_rank1_vs "\hspace{0.2cm} ADC vote share"

lab var born_in_district "\hspace{0.2cm} Candidate: Born in district"
lab var ed_secondary "\hspace{0.2cm} Candidate: Secondary education"
lab var ed_university "\hspace{0.2cm} Candidate: University education"
lab var ed_vocational "\hspace{0.2cm} Candidate: Vocational education"
lab var nec_veto "\hspace{0.2cm} NEC veto"
lab var n_ps "\hspace{0.2cm} Number of polling stations"
lab var occ_main_farmer "\hspace{0.2cm} Main occupation: Farmer"
lab var occ_main_bureaucrat_senior "\hspace{0.2cm} Main occupation: Bureaucrat (senior)"
lab var occ_any_bureaucrat_senior "\hspace{0.2cm} Prior occupation: Bureaucrat (senior)"
lab var occ_any_private "\hspace{0.2cm} Prior occupation: Business"
lab var occ_any_religious "\hspace{0.2cm} Prior occupation: Religious"
lab var occ_any_bureaucrat_junior "\hspace{0.2cm} Prior occupation: Bureaucrat (junior)"
lab var trad_authority "\hspace{0.2cm} Candidate: Traditional authority"
lab var d_town_major "\hspace{0.2cm} Distance to major town (km)"
lab var ln_n_pre_sec "\hspace{0.2cm} Log Secondary schools"
lab var minister "\hspace{0.2cm} Candidate: Minister"
lab var reg_voters "\hspace{0.2cm} Number of registered voters"
lab var adc_votes "\hspace{0.2cm} Candidate: ADC votes received"
lab var adc_vs "\hspace{0.2cm} Candidate: ADC vote share"
lab var c57_pop "\hspace{0.2cm} Population (1957)"
lab var c67_eth1_share "\hspace{0.2cm} 1st largest ethnicity population share"
lab var c67_eth2_share "\hspace{0.2cm} 2nd largest ethnicity population share"
lab var c67_eth3_share "\hspace{0.2cm} 3rd largest ethnicity population share"
lab var latitude "\hspace{0.2cm} Latitude"
lab var longitude "\hspace{0.2cm} Longitude"
lab var area "\hspace{0.2cm} Area (sq. km)"
lab var c67_christian "\hspace{0.2cm} Share Christian"
lab var leader_union_coop "\hspace{0.2cm} Candidate: Union/Cooperative leader"
lab var leader_local_appointed "\hspace{0.2cm} Candidate: Local appointed leader"
lab var leader_local_elected "\hspace{0.2cm} Candidate: Local elected leader"
lab var c67_ethNyamwezi_share "\hspace{0.2cm} Share Nyamwezi/Sukuma"

encode district, gen(i_district)
encode region, gen(i_region)
encode region_year, gen(i_region_year)

********************************************************************************
*** First stage regressions ****************************************************
********************************************************************************

local dv "treat"

local bl_vars_cand "adc_vs adc_votes incumbent minister male born_in_district occ_any_* occ_main_* ed_* leader_* trad_authority"
local bl_vars_constit "nec_veto reg_voters n_ps n_cand adc_votes_total area longitude latitude d_road d_road_any d_rail d_town_* d_afcap c67_christia* c67_musli* c67_eth*_*  c67_rel_hhi c57_pop" 
local bl_vars_lpg "ln_n_pre_pri ln_n_pre_sec ln_n_pre_disp ln_n_pre_health ln_n_pre_wp"

foreach var in `dv' {
	
	reghdfe `var' J, a(year) vce(robust)
		summ `var' if e(sample)==1 & J==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		test J
			estadd scalar fs = `r(F)'
		estadd local ctrl "$\times$"
		estadd local fe "$\times$"
		est sto `var'_1a
		
	dsregress `var' J, controls((i.year) c.(`bl_vars_cand' `bl_vars_constit' `bl_vars_lpg')) rseed(94305) vce(robust) selection(cv)
		summ `var' if e(sample)==1 & J==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		test J
			estadd scalar fs = `r(chi2)'
		estadd local ctrl "$\checkmark$"
		estadd local fe "$\times$"
		est sto `var'_1b		
		
	reg `var' J `e(controls_sel)', robust
		summ `var' if e(sample)==1 & J==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		estadd local ctrl "$\checkmark$"
		estadd local fe "$\times$"
		est sto `var'_1b_ctrl	
		
	reghdfe `var' J i.i_region, a(year) vce(robust)
		sum `var' if e(sample)==1 & J==0
		estadd local ctrl "$\times$"
		estadd local fe "$\checkmark$"
		estadd scalar DV_Mean = `r(mean)'
		estadd scalar DV_SD = `r(sd)'	
			test J
			estadd scalar fs = `r(F)'		
		est sto `var'_2a
		
	dsregress `var' J, controls((i.year i.i_region) c.(`bl_vars_cand' `bl_vars_constit' `bl_vars_lpg')) rseed(94305) vce(robust) selection(cv)
		sum `var' if e(sample)==1 & J==0
		estadd local fe "$\checkmark$"
		estadd local ctrl "$\checkmark$"		
		estadd scalar DV_Mean = `r(mean)'
		estadd scalar DV_SD = `r(sd)'	
			test J
			estadd scalar fs = `r(chi2)'		
		est sto `var'_2b
		
	reg `var' J `e(controls_sel)', robust
		sum `var' if e(sample)==1 & J==0
		estadd local fe "$\checkmark$"
		estadd local ctrl "$\checkmark$"		
		estadd scalar DV_Mean = `r(mean)'
		estadd scalar DV_SD = `r(sd)'	
		est sto `var'_2b_ctrl	
		
	foreach yr in 1965 1970 {
		
		reghdfe `var' J if year==`yr', a(year) vce(robust)
			sum `var' if e(sample)==1 & J==0
			estadd local ctrl "$\times$"
			estadd local fe "$\times$"
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
				test J
				estadd scalar fs = `r(F)'
			est sto `var'_1a_`yr'
			
		dsregress `var' J if year==`yr', controls(c.(`bl_vars_cand' `bl_vars_constit' `bl_vars_lpg')) rseed(94305) vce(robust) selection(cv)
			sum `var' if e(sample)==1 & J==0
			estadd local ctrl "$\checkmark$"	
			estadd local fe "$\times$"
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
				test J
				estadd scalar fs = `r(chi2)'
			est sto `var'_1b_`yr'	
			
		reg `var' J `e(controls_sel)' if year==`yr', robust
			sum `var' if e(sample)==1 & J==0
			estadd local ctrl "$\checkmark$"
			estadd local fe "$\times$"
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
			est sto `var'_1b_`yr'_ctrl	
		
		reghdfe `var' J i.i_region if year==`yr', a(year) vce(robust)
			sum `var' if e(sample)==1 & J==0
			estadd local ctrl "$\times$"
			estadd local fe "$\checkmark$"
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
				test J
				estadd scalar fs = `r(F)'
			est sto `var'_2a_`yr'
			
		dsregress `var' J if year==`yr', controls((i.i_region) c.(`bl_vars_cand' `bl_vars_constit' `bl_vars_lpg')) rseed(94305) vce(robust) selection(cv)
			sum `var' if e(sample)==1 & J==0
			estadd local fe "$\checkmark$"
			estadd local ctrl "$\checkmark$"	
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
				test J
				estadd scalar fs = `r(chi2)'
			est sto `var'_2b_`yr'
			
		reg `var' J `e(controls_sel)' i.i_region if year==`yr', robust
			sum `var' if e(sample)==1 & J==0
			estadd local ctrl "$\checkmark$"
			estadd local fe "$\times$"
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
			est sto `var'_2b_`yr'_ctrl				
			
	}	
}

local numbers "& (1) & (2) & (3) & (4) \\ \midrule"

esttab treat_1a treat_1b treat_2a treat_2b using "Table 4.tex",  ///
	replace nonumber posthead("`numbers'") nomtitles b(3) se label f nogaps booktabs noobs compress ///
	nobaselevels keep(J) stats(ctrl fe fs DV_Mean DV_SD N, labels("\midrule Controls" "Region FE" "F-statistic" "DV Mean" "DV SD" "Observations") ///		
	fmt(0 3 2 3 3 0)) nolines nonumber nostar

esttab treat_1a treat_1b_ctrl treat_2a treat_2b_ctrl using "Table A24.tex",  ///
	replace nonumber posthead("`numbers'") nomtitles b(3) se label f nogaps booktabs noobs compress ///
	nobaselevels drop(*i_regio* *year* _cons) stats(ctrl fe DV_Mean DV_SD N, labels("\midrule Controls" "Region FE" "DV Mean" "DV SD" "Observations") ///		
	fmt(0 3 2 3 0)) nolines nonumber nostar noconstant
		
local titles "& \multicolumn{4}{c}{1965} & \multicolumn{4}{c}{1970} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
	
local numbers " & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"

esttab treat_1a_1965 treat_1b_1965 treat_2a_1965 treat_2b_1965 treat_1a_1970 treat_1b_1970 treat_2a_1970 treat_2b_1970 using "Table A8.tex",  ///
	replace nonumber posthead("`titles'" "`lines'"  "`numbers'") nomtitles b(3) se label f nogaps booktabs noobs compress ///
	nobaselevels keep(J) stats(ctrl fe fs DV_Mean DV_SD N, labels("\midrule Controls" "Region FE" "F-statistic" "DV Mean" "DV SD" "Observations") ///		
	fmt(0 0 2 3 3 0)) nolines nonumber nostar
	
esttab treat_1a_1965 treat_1b_1965_ctrl treat_2a_1965 treat_2b_1965_ctrl treat_1a_1970 treat_1b_1970_ctrl treat_2a_1970 treat_2b_1970_ctrl using "Table A25.tex",  ///
	replace nonumber posthead("`titles'" "`lines'"  "`numbers'") nomtitles b(3) se label f nogaps booktabs noobs compress ///
	nobaselevels drop(*i_regio* _cons) stats(ctrl fe DV_Mean DV_SD N, labels("\midrule Controls" "Region FE" "DV Mean" "DV SD" "Observations") ///		
	fmt(0 0 2 3 0)) nolines nonumber nostar


foreach var in treat {
				
	reghdfe `var' c.J##c.z_adc_rank1_vs, a(year) vce(robust)
		sum `var' if e(sample)==1 & J==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		test J
			estadd scalar fs = `r(F)'
		estadd local ctrl "$\times$"
		estadd local fe "$\times"
		est sto `var'_het_1a
		
	dsregress `var' c.J##c.z_adc_rank1_vs, controls((i.year) c.(`bl_vars_cand' `bl_vars_constit' `bl_vars_lpg')##i.i_region) rseed(94305) vce(robust) selection(cv) 
		sum `var' if e(sample)==1 & J==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		test J
			estadd scalar fs = `r(chi2)'
		estadd local ctrl "$\checkmark$"
		estadd local fe "$\times$"
		est sto `var'_het_1b
		
	reg `var' c.J##c.z_adc_rank1_vs `e(controls_sel)', robust
		sum `var' if e(sample)==1 & J==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		estadd local ctrl "$\checkmark$"
		estadd local fe "$\times$"
		est sto `var'_het_1b_ctrl			
}

	
local numbers " & (1) & (2) & (3) & (4) \\ \midrule"

esttab treat_1a treat_1b treat_het_1a treat_het_1b using "Table A20.tex", ///
	replace nonumber posthead("`numbers'") nomtitles b(3) se label f nogaps booktabs noobs compress /// 
	nobaselevels keep(J c.J#c.z_adc_rank1_vs) stats(ctrl fs DV_Mean DV_SD N, labels("\midrule Controls" "F-statistic" "DV Mean" "DV SD" "Observations") ///		
	fmt(0 2 3 3 0)) nolines nonumber nostar
	
esttab treat_1a treat_1b_ctrl treat_het_1a treat_het_1b_ctrl using "Table A26.tex", ///
	replace nonumber posthead("`numbers'") nomtitles b(3) se label f nogaps booktabs noobs compress ///
	nobaselevels drop(*year* *i_regio* _cons) stats(ctrl fs DV_Mean DV_SD N, labels("\midrule Controls" "F-statistic" "DV Mean" "DV SD" "Observations") ///		
	fmt(0 2 3 3 0)) nolines nonumber nostar

	
********************************************************************************
*** Instrumental variables regressions *****************************************
********************************************************************************

local outvars "pri sec disp health wp other"

foreach var in `outvars' {

	local prefix "ln urb_ln ihs ln_pc"	

	foreach prefix in `prefix' {
		
		ivreghdfe `prefix'_n_post_`var' (treat=J) , a(year) robust
			sum `prefix'_n_post_`var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
				estadd scalar boot_p = `r(p)'
			estadd local fe "$\times$"
			estadd local wgt "$\times$"
			est sto `prefix'_n_post_`var'_1	
		
		ivreghdfe `prefix'_n_post_`var' (treat=J) [aweight=wgt], a(year) robust
			sum `prefix'_n_post_`var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
				estadd scalar boot_p = `r(p)'
			estadd local fe "$\times$"
			estadd local wgt "$\checkmark$"
			est sto `prefix'_n_post_`var'_2
			
		ivreghdfe `prefix'_n_post_`var' (treat=J) i.i_region, a(year) robust
			summ `prefix'_n_post_`var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
				estadd scalar boot_p = `r(p)'
			estadd local fe "$\checkmark$"	
			estadd local wgt "$\times$"
			est sto `prefix'_n_post_`var'_3	
		
		ivreghdfe `prefix'_n_post_`var' (treat=J) i.i_region [aweight=wgt], a(year) robust
			sum `prefix'_n_post_`var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
				estadd scalar boot_p = `r(p)'
			estadd local fe "$\checkmark$"
			estadd local wgt "$\checkmark$"
			est sto `prefix'_n_post_`var'_4
	}		
}

local titles "& \multicolumn{4}{c}{Primary schools} & \multicolumn{4}{c}{Other local public goods} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
local numbers "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
esttab ln_n_post_pri_1 ln_n_post_pri_2 ln_n_post_pri_3 ln_n_post_pri_4 /// 
	ln_n_post_other_1 ln_n_post_other_2 ln_n_post_other_3 ln_n_post_other_4 ///
	using "Table 5.tex",  ///
		replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p wgt fe DV_Mean DV_SD widstat N, labels(" " "\midrule Weights" "Region FE" "DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 0 2 2 1 0)) nolines

local titles "& \multicolumn{4}{c}{Secondary schools} & \multicolumn{4}{c}{Health facilities} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
local numbers " & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
esttab ln_n_post_sec_1 ln_n_post_sec_2 ln_n_post_sec_3 ln_n_post_sec_4 /// 
	ln_n_post_health_1 ln_n_post_health_2 ln_n_post_health_3 ln_n_post_health_4 /// 
	using "Table A9.tex",  ///
		replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p wgt fe DV_Mean DV_SD widstat N, labels(" " "\midrule Weights" "Region FE" "DV Mean" "DV SD" "FS F-statistic" "Observations") ///
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 0 2 2 1 0)) nolines //0 0 

local titles "\\ & \multicolumn{4}{c}{Dispensaries} & \multicolumn{4}{c}{Water points} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
local numbers " & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"	
esttab ln_n_post_disp_1 ln_n_post_disp_2 ln_n_post_disp_3 ln_n_post_disp_4 /// 
	ln_n_post_wp_1 ln_n_post_wp_2 ln_n_post_wp_3 ln_n_post_wp_4 /// 
	using "Table A9.tex",  ///
		append nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p wgt fe DV_Mean DV_SD widstat N, labels(" " "\midrule Weights" "Region FE" "DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 0 2 2 1 0)) nolines //0 0 


local outvars "pri other"

foreach var in `outvars' {
	ivreghdfe ln_n_post_`var' (treat=J) if nec_veto==0, a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local wgt "$\times$"	
		estadd local fe "$\times$"
		est sto r_`var'_1	

	ivreghdfe ln_n_post_`var' (treat=J) [aweight=wgt] if nec_veto==0, a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local wgt "$\checkmark$"
		estadd local fe "$\times$"	
		est sto r_`var'_2
			
	ivreghdfe ln_n_post_`var' (treat=J) i.i_region if nec_veto==0, a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local wgt "$\times$"
		estadd local fe "$\checkmark$"	
		est sto r_`var'_3	
		
	ivreghdfe ln_n_post_`var' (treat=J) i.i_region [aweight=wgt] if nec_veto==0, a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local wgt "$\checkmark$"
		estadd local fe "$\checkmark$"
		est sto r_`var'_4
}

local titles "& \multicolumn{4}{c}{Primary schools} & \multicolumn{4}{c}{Other local public goods} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"

local numbers "\textbf{A. Including towns} & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
esttab urb_ln_n_post_pri_1 urb_ln_n_post_pri_2 urb_ln_n_post_pri_3 urb_ln_n_post_pri_4 /// 
	urb_ln_n_post_other_1 urb_ln_n_post_other_2 urb_ln_n_post_other_3 urb_ln_n_post_other_4 ///
	using "Table A10.tex",  ///
		replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p wgt fe DV_Mean DV_SD widstat N, labels(" " "\midrule Weights" "Region FE" "DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 0 2 2 1 0)) nolines		

local numbers "\\ \textbf{B. Excluding NEC vetos} & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
esttab r_pri_1 r_pri_2 r_pri_3 r_pri_4 /// 
	r_other_1 r_other_2 r_other_3 r_other_4 ///
	using "Table A10.tex",  ///
		append nonumber posthead("`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p wgt fe DV_Mean DV_SD widstat N, labels(" " "\midrule Weights" "Region FE" "DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 0 2 2 1 0)) nolines //0 0 

	
local outvars "pri other"

foreach var in `outvars' {
	
	ivpoisson cfunction n_post_`var' i.year (treat=J)
		summ n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		test treat
			estadd scalar p = `r(p)'			
		test J
			estadd scalar F = `r(chi2)'
		est sto pois_`var'_1	
		
	ivpoisson cfunction n_post_`var' i.year (treat=J) [aweight=wgt]
		summ n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		test treat
			estadd scalar p = `r(p)'	
		test J
			estadd scalar F = `r(chi2)'
		est sto pois_`var'_2

	ivpoisson cfunction n_post_`var' i.i_region i.year (treat=J) 		
		summ n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		test treat
			estadd scalar p = `r(p)'	
		test treat
			estadd scalar F = `r(chi2)'
		est sto pois_`var'_3
			
	ivpoisson cfunction n_post_`var' i.i_region i.year (treat=J) [aweight=wgt]
		summ n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		test treat
			estadd scalar p = `r(p)'	
		test treat
			estadd scalar F = `r(chi2)'
		est sto pois_`var'_4		
	
}

local titles "& \multicolumn{4}{c}{Primary schools} & \multicolumn{4}{c}{Other local public goods} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"

local numbers "\textbf{A. Inverse hyperbolic sine}  & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
esttab ihs_n_post_pri_1 ihs_n_post_pri_2 ihs_n_post_pri_3 ihs_n_post_pri_4 /// 
	ihs_n_post_other_1 ihs_n_post_other_2 ihs_n_post_other_3 ihs_n_post_other_4 ///
	using "Table A11.tex",  ///
		replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p DV_Mean DV_SD, labels(" " "\midrule DV Mean" "DV SD") /// 
		layout("\$\left[\text{@}\right]\$" "@" "@") fmt(2 2 2)) nolines

local numbers "\\ \textbf{B. Non-transformed count}  & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
esttab pois_pri_1 pois_pri_2 pois_pri_3 pois_pri_4 /// 
	pois_other_1 pois_other_2 pois_other_3 pois_other_4 ///
	using "Table A11.tex",  ///
		append nonumber posthead("`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(p DV_Mean DV_SD, labels(" " "\midrule DV Mean" "DV SD") /// 
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 2 2)) nolines nostar eqlabels(" " " ")
		
local numbers "\\ \textbf{C. Log per capita}  & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"	
esttab ln_pc_n_post_pri_1 ln_pc_n_post_pri_2 ln_pc_n_post_pri_3 ln_pc_n_post_pri_4 /// 
	ln_pc_n_post_other_1 ln_pc_n_post_other_2 ln_pc_n_post_other_3 ln_pc_n_post_other_4 ///
	using "Table A11.tex",  ///
		append nonumber posthead("`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p DV_Mean DV_SD wgt fe, labels(" " "\midrule DV Mean" "DV SD" "Weights" "Region FE" ) /// 
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 2 2 0 0)) nolines nostar eqlabels(" " " ")


local outvars "pri other"
local years "1965 1970"

foreach yr in `years' {
	foreach var in `outvars' {
	
	ivreghdfe ln_n_post_`var' (treat=J) if year==`yr', a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local year "`yr'"
		estadd local fe "$\times$"
		estadd local wgt "$\times$"			
		est sto `var'_`yr'_1	
		
	ivreghdfe ln_n_post_`var' (treat=J) [aweight=wgt] if year==`yr', a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local year "`yr'"
		estadd local fe "$\times$"
		estadd local wgt "$\checkmark$"
		est sto `var'_`yr'_2

	ivreghdfe ln_n_post_`var' (treat=J) i.i_region if year==`yr', a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local year "`yr'"
		estadd local fe "$\checkmark$"
		estadd local wgt "$\times$"
		est sto `var'_`yr'_3	
		
	ivreghdfe ln_n_post_`var' (treat=J) i.i_region [aweight=wgt] if year==`yr', a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local year "`yr'"
		estadd local wgt "$\checkmark$"		
		estadd local fe "$\checkmark$"		
		est sto `var'_`yr'_4	
	}
}

local titles "& \multicolumn{4}{c}{Primary schools} & \multicolumn{4}{c}{Other local public goods} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
local numbers " & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"

esttab pri_1965_1 pri_1965_2 pri_1970_1 pri_1970_2 /// 
other_1965_1 other_1965_2 other_1970_1 other_1970_2 ///
using "Table A23.tex",  ///
	replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
	nobaselevels keep(treat) stats(boot_p year wgt DV_Mean DV_SD widstat N, labels(" " "\midrule Year" "Weights" "DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
	layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 0 2 2 1 0)) nolines

	
estimates drop _all

local outvars "pri other sec disp health wp"

foreach var in `outvars' {
	
	ivreghdfe ln_n_post_`var' (treat=J), a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p1 = `r(p)'
		estadd local fe "$\times$"
		estadd local wgt "$\times$"
		est sto `var'_1	
		
	ivreghdfe ln_n_post_`var' (treat=J) [aweight=wgt], a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p1 = `r(p)'
		estadd local fe "$\times$"
		estadd local wgt "$\checkmark$"
		est sto `var'_2
		
	local hets "z_adc_rank1_vs"
	
	foreach het in `hets' {
		
		ivreghdfe ln_n_post_`var' c.`het' (c.treat c.treat#c.`het'=c.J c.J#c.`het'), a(year) robust
			summ ln_n_post_`var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest {treat} {c.treat#c.`het'}, statistic(t) reps(5000) seed(94305) nograph
				estadd scalar boot_p1 = `r(p_1)'
				estadd scalar boot_p2 = `r(p_2)'
			estadd local wgt "$\times$"
			est sto `var'_`het'_1
			
		ivreghdfe ln_n_post_`var' c.`het' (c.treat c.treat#c.`het'=c.J c.J#c.`het') [aweight=wgt], a(year) robust
			summ ln_n_post_`var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest {treat} {c.treat#c.`het'}, statistic(t) reps(5000) seed(94305) nograph
				estadd scalar boot_p1 = `r(p_1)'
				estadd scalar boot_p2 = `r(p_2)'
			estadd local wgt "$\checkmark$"
			est sto `var'_`het'_2		
		
		}
	
	local het "terc_adc_rank1_vs"
	
	foreach het in `het' {
		
		ivreghdfe ln_n_post_`var' i.`het' (c.treat c.treat#i.`het'=c.J c.J#i.`het'), a(year) robust
			summ ln_n_post_`var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest {treat} {c.treat#2.`het'} {c.treat#3.`het'}, statistic(t) reps(5000) seed(94305) nograph
				estadd scalar boot_p1 = `r(p_1)'
				estadd scalar boot_p2 = `r(p_2)'
				estadd scalar boot_p3 = `r(p_3)'		
			est sto `var'_`het'_3
			
		ivreghdfe ln_n_post_`var' i.`het' (c.treat c.treat#i.`het'=c.J c.J#i.`het') [aweight=wgt], a(year) robust
			summ ln_n_post_`var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest {treat} {c.treat#2.`het'} {c.treat#3.`het'}, statistic(t) reps(5000) seed(94305) nograph
				estadd scalar boot_p1 = `r(p_1)'
				estadd scalar boot_p2 = `r(p_2)'
				estadd scalar boot_p3 = `r(p_3)'
			est sto `var'_`het'_4			
		
	}
}


local titles "& \multicolumn{4}{c}{Primary schools} & \multicolumn{4}{c}{Other local public goods} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
local numbers "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"

esttab pri_1 pri_2 pri_z_adc_rank1_vs_1 pri_z_adc_rank1_vs_2 /// 
	other_1 other_2 other_z_adc_rank1_vs_1 other_z_adc_rank1_vs_2 ///
	using "Table 6.tex",  ///
		replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat c.treat#c.*) stats(boot_p1 boot_p2 wgt DV_Mean DV_SD N, labels(" " " " "\midrule Weights" "DV Mean" "DV SD" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "\$\left[\text{@}\right]\$" "@") fmt(2 2 0 2 2 0 0)) nolines 

esttab pri_1 pri_2 pri_z_adc_rank1_vs_1 pri_z_adc_rank1_vs_2 /// 
	other_1 other_2 other_z_adc_rank1_vs_1 other_z_adc_rank1_vs_2 ///
	using "Table A27.tex",  ///
		replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat c.treat#c.* z_a*) stats(boot_p1 boot_p2 wgt DV_Mean DV_SD N, labels(" " " " "\midrule Weights" "DV Mean" "DV SD" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "\$\left[\text{@}\right]\$" "@") fmt(2 2 0 2 2 0 0)) nolines 
	
local titles "& \multicolumn{4}{c}{Primary schools} & \multicolumn{4}{c}{Other local public goods} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
local numbers "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"

esttab pri_1 pri_2 pri_terc_adc_rank1_vs_3 pri_terc_adc_rank1_vs_4 /// 
	other_1 other_2 other_terc_adc_rank1_vs_3 other_terc_adc_rank1_vs_4 ///
	using "Table A22.tex",  ///
		replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat *.terc_adc_rank1_vs#c.treat) coeflabel(T "\hspace{0.2cm} $\widehat{\text{Preferred}}$" 2.terc_adc_rank1_vs#c.treat "\hspace{0.2cm} $\widehat{\text{Preferred}} \times \text{ADC}^{1}_{p33-p66}$" 3.terc_adc_rank1_vs#c.treat "\hspace{0.2cm} $\widehat{\text{Preferred}} \times \text{ADC}^{1}\_{p67-p100}$") ///
		stats(boot_p1 boot_p2 boot_p3 wgt DV_Mean DV_SD N, labels(" " " " " " "\midrule Weights" "DV Mean" "DV SD" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "\$\left[\text{@}\right]\$" "\$\left[\text{@}\right]\$" "@") fmt(2 2 2 0 2 2 0 0)) nolines substitute(\_ _)

esttab pri_1 pri_2 pri_terc_adc_rank1_vs_3 pri_terc_adc_rank1_vs_4 /// 
	other_1 other_2 other_terc_adc_rank1_vs_3 other_terc_adc_rank1_vs_4 ///
	using "Table A29.tex",  ///
		replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat *.terc_adc_rank1_vs#c.treat *terc*) coeflabel(T "\hspace{0.2cm} $\widehat{\text{Preferred}}$" 2.terc_adc_rank1_vs#c.treat "\hspace{0.2cm} $\widehat{\text{Preferred}} \times \text{ADC}^{1}_{p33-p66}$" 3.terc_adc_rank1_vs#c.treat "\hspace{0.2cm} $\widehat{\text{Preferred}} \times \text{ADC}^{1}\_{p67-p100}$") ///
		stats(boot_p1 boot_p2 boot_p3 wgt DV_Mean DV_SD N, labels(" " " " " " "\midrule Weights" "DV Mean" "DV SD" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "\$\left[\text{@}\right]\$" "\$\left[\text{@}\right]\$" "@") fmt(2 2 2 0 2 2 0 0)) nolines substitute(\_ _)
		
	
local titles "& \multicolumn{4}{c}{Secondary schools} & \multicolumn{4}{c}{Health facilities} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
local numbers " & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"

esttab sec_1 sec_2 sec_z_adc_rank1_vs_1 sec_z_adc_rank1_vs_2 /// 
	health_1 health_2 health_z_adc_rank1_vs_1 health_z_adc_rank1_vs_2 /// 
	using "Table A21.tex",  ///
		replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs nostar compress ///
		nobaselevels keep(treat c.treat#c.*) stats(boot_p1 boot_p2 wgt DV_Mean DV_SD N, labels(" " " " "\midrule Weights" "DV Mean" "DV SD" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "\$\left[\text{@}\right]\$" "@") fmt(2 2 0 2 2 0 0)) nolines 

local titles "\\ & \multicolumn{4}{c}{Dispensaries} & \multicolumn{4}{c}{Water points} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
local numbers " & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"	

esttab disp_1 disp_2 disp_z_adc_rank1_vs_1 disp_z_adc_rank1_vs_2 /// 
	wp_1 wp_2 wp_z_adc_rank1_vs_1 wp_z_adc_rank1_vs_2 /// 
	using "Table A21.tex",  ///
		append nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs nostar compress ///
		nobaselevels keep(treat c.treat#c.*) stats(boot_p1 boot_p2 wgt DV_Mean DV_SD N, labels(" " " " "\midrule Weights" "DV Mean" "DV SD" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "\$\left[\text{@}\right]\$" "@") fmt(2 2 0 2 2 0 0)) nolines 


local titles "& \multicolumn{4}{c}{Secondary schools} & \multicolumn{4}{c}{Health facilities} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
local numbers " & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
	
esttab sec_1 sec_2 sec_z_adc_rank1_vs_1 sec_z_adc_rank1_vs_2 /// 
	health_1 health_2 health_z_adc_rank1_vs_1 health_z_adc_rank1_vs_2 /// 
	using "Table A28.tex",  ///
		replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs nostar compress ///
		nobaselevels keep(treat c.treat#c.* z_adc*) stats(boot_p1 boot_p2 wgt DV_Mean DV_SD N, labels(" " " " "\midrule Weights" "DV Mean" "DV SD" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "\$\left[\text{@}\right]\$" "@") fmt(2 2 0 2 2 0 0)) nolines

local titles "\\ & \multicolumn{4}{c}{Dispensaries} & \multicolumn{4}{c}{Water points} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
local numbers "  & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"	

esttab disp_1 disp_2 disp_z_adc_rank1_vs_1 disp_z_adc_rank1_vs_2 /// 
	wp_1 wp_2 wp_z_adc_rank1_vs_1 wp_z_adc_rank1_vs_2 /// 
	using "Table A28.tex",  ///
		append nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs nostar compress ///
		nobaselevels keep(treat c.treat#c.* z_adc*) stats(boot_p1 boot_p2 wgt DV_Mean DV_SD N, labels(" " " " "\midrule Weights" "DV Mean" "DV SD" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "\$\left[\text{@}\right]\$" "@") fmt(2 2 0 2 2 0 0)) nolines
			

local outvars "switch new"
local quarters "1 2 3 4"
local prefixes "ln any"

foreach prefix in `prefixes' {
	
	foreach quarter in `quarters' {
		
		foreach outvar in `outvars'{
		
			quietly ivreghdfe `prefix'_staff_n_`outvar'_post_q`quarter' (treat=J), a(year) robust
				summ `prefix'_staff_n_`outvar'_post_q`quarter' if e(sample)==1 & treat==0
					estadd scalar DV_Mean = `r(mean)'
					estadd scalar DV_SD = `r(sd)'	
				boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
					estadd scalar boot_p = `r(p)'
				estadd local q "`quarter'"
				est sto `prefix'_`outvar'_q`quarter'_1
			
			quietly ivreghdfe `prefix'_staff_n_`outvar'_post_q`quarter' (treat=J) if incumbent==0, a(year) robust
				summ `prefix'_staff_n_`outvar'_post_q`quarter' if e(sample)==1 & treat==0
					estadd scalar DV_Mean = `r(mean)'
					estadd scalar DV_SD = `r(sd)'	
				boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
					estadd scalar boot_p = `r(p)'
				estadd local q "`quarter'"
				est sto `prefix'_`outvar'_q`quarter'_2
				
			quietly ivreghdfe `prefix'_staff_n_`outvar'_post_q`quarter' (treat=J) if dsm_region==0, a(year) robust
				summ `prefix'_staff_n_`outvar'_post_q`quarter' if e(sample)==1 & treat==0
					estadd scalar DV_Mean = `r(mean)'
					estadd scalar DV_SD = `r(sd)'	
				boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
					estadd scalar boot_p = `r(p)'
				estadd local q "`quarter'"
				est sto `prefix'_`outvar'_q`quarter'_3
				
			quietly ivreghdfe `prefix'_staff_n_`outvar'_post_q`quarter' (treat=J) if has_office==1, a(year) robust
				summ `prefix'_staff_n_`outvar'_post_q`quarter' if e(sample)==1 & treat==0
					estadd scalar DV_Mean = `r(mean)'
					estadd scalar DV_SD = `r(sd)'	
				boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
					estadd scalar boot_p = `r(p)'
				estadd local q "`quarter'"
				est sto `prefix'_`outvar'_q`quarter'_4
				
		}
	}
}



local titles "& \multicolumn{4}{c}{New employees} & \multicolumn{4}{c}{New postings} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
local numbers "\textbf{A. Binary outcome} & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
esttab any_new_q1_1 any_new_q2_1 any_new_q3_1 any_new_q4_1 any_switch_q1_1 any_switch_q2_1 any_switch_q3_1 any_switch_q4_1 using "Table A17.tex", ///
	replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p q DV_Mean DV_SD widstat N, labels(" " "\midrule Quarters" "DV Mean" "DV SD" "FS F-statistic" "Observations") ///
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 2 2 1 0)) nolines 

local numbers "\\ \textbf{B. Log-transformed outcome} & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
esttab ln_new_q1_1 ln_new_q2_1 ln_new_q3_1 ln_new_q4_1 ln_switch_q1_1 ln_switch_q2_1 ln_switch_q3_1 ln_switch_q4_1 using "Table A17.tex", ///
	append nonumber posthead("`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p q DV_Mean DV_SD widstat N, labels(" " "\midrule Quarters" "DV Mean" "DV SD" "FS F-statistic" "Observations") ///
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 2 2 1 0)) nolines 

local numbers "\\ \textbf{C. Constituencies with any office} & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
esttab any_new_q1_4 any_new_q2_4 any_new_q3_4 any_new_q4_4 any_switch_q1_4 any_switch_q2_4 any_switch_q3_4 any_switch_q4_4 using "Table A17.tex", ///
	append  nonumber posthead("`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p q DV_Mean DV_SD widstat N, labels(" " "\midrule Quarters" "DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 2 2 1 0)) nolines 

local numbers "\\ \textbf{D. Excluding Dar-es-Salaam} & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
esttab any_new_q1_3 any_new_q2_3 any_new_q3_3 any_new_q4_3 any_switch_q1_3 any_switch_q2_3 any_switch_q3_3 any_switch_q4_3 using "Table A17.tex", ///
	append  nonumber posthead("`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p q DV_Mean DV_SD widstat N, labels(" " "\midrule Quarters" "DV Mean" "DV SD" "FS F-statistic" "Observations") ///
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 2 2 1 0)) nolines 
	
local numbers "\\ \textbf{E. Excluding elite-preferred incumbents} & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
esttab any_new_q1_2 any_new_q2_2 any_new_q3_2 any_new_q4_2 any_switch_q1_2 any_switch_q2_2 any_switch_q3_2 any_switch_q4_2 using "Table A17.tex", ///
	append  nonumber posthead("`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p q DV_Mean DV_SD widstat N, labels(" " "\midrule Quarters" "DV Mean" "DV SD" "FS F-statistic" "Observations") ///
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 2 2 1 0)) nolines 
		

local vars "pri other"

foreach var in `vars' {

	foreach spill in r d {
		
		ivreghdfe ln_n_post_`var' (treat s_treat_`spill'=J s_J_`spill'), a(year) robust
			summ ln_n_post_`var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest {treat} {s_treat_`spill'}, statistic(t) reps(5000) seed(94305) nograph
				estadd scalar boot_p1 = `r(p_1)'
				estadd scalar boot_p2 = `r(p_2)'
			estadd local fe "$\times$"
			estadd local wgt "$\times$"
			est sto `var'_`spill'_1	
		
		ivreghdfe ln_n_post_`var' (treat s_treat_`spill'=J s_J_`spill') [aweight=wgt], a(year) robust
			summ ln_n_post_`var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest {treat} {s_treat_`spill'}, statistic(t) reps(5000) seed(94305) nograph
				estadd scalar boot_p1 = `r(p_1)'
				estadd scalar boot_p2 = `r(p_2)'
			estadd local fe "$\times$"
			estadd local wgt "$\checkmark$"
			est sto `var'_`spill'_2
			
		ivreghdfe ln_n_post_`var' (treat s_treat_`spill'=J s_J_`spill') i.i_region, a(year) robust
			summ ln_n_post_`var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest {treat} {s_treat_`spill'}, statistic(t) reps(5000) seed(94305) nograph
				estadd scalar boot_p1 = `r(p_1)'
				estadd scalar boot_p2 = `r(p_2)'
			estadd local fe "$\checkmark$"
			estadd local wgt "$\times$"
			est sto `var'_`spill'_3	
		
		ivreghdfe ln_n_post_`var' (treat s_treat_`spill'=J s_J_`spill') i.i_region [aweight=wgt], a(year) robust
			summ ln_n_post_`var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest {treat} {s_treat_`spill'}, statistic(t) reps(5000) seed(94305) nograph
				estadd scalar boot_p1 = `r(p_1)'
				estadd scalar boot_p2 = `r(p_2)'
			estadd local fe "$\checkmark$"
			estadd local wgt "$\checkmark$"
			est sto `var'_`spill'_4
		
	}
	
}
	
local titles "& \multicolumn{4}{c}{Primary schools} & \multicolumn{4}{c}{Other local public goods} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
local numbers "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"

esttab pri_d_1 pri_d_2 pri_r_1 pri_r_2 ///
other_d_1 other_d_2 other_r_1 other_r_2 ///
using "Table A16.tex",  ///
	replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
	nobaselevels keep(treat s_treat_*) stats(boot_p1 boot_p2 wgt DV_Mean DV_SD N, labels(" " " " "\midrule Weights" "DV Mean" "DV SD" "Observations") /// 
	layout("\$\left[\text{@}\right]\$" "\$\left[\text{@}\right]\$" "@") fmt(2 2 0 2 2 0 0)) nolines nostar substitute(\_ _)


********************************************************************************
**** District-level instrumental variables regressions *************************
********************************************************************************

clear all

use "$root/Data/analysis_3.dta"
cd "$output"

encode region, gen(i_region)
label variable s_treat "\hspace{0.2cm} $\widehat{\text{Share elite-preferred}}$"

local outvars "pri other"

foreach var in `outvars' {
	
	ivreghdfe ln_n_post_`var' (s_treat=s_J), a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & s_treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest s_treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local wgt "$\times$"
		estadd local fe "$\times$"
		est sto `var'_1	
		
	ivreghdfe ln_n_post_`var' (s_treat=s_J) [aweight=wgt], a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & s_treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest s_treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local wgt "$\checkmark$"
		estadd local fe "$\times$"
		est sto `var'_2

	ivreghdfe ln_n_post_`var' (s_treat=s_J) i.i_region, a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & s_treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest s_treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local wgt "$\times$"
		estadd local fe "$\checkmark$"
		est sto `var'_3
		
	ivreghdfe ln_n_post_`var' (s_treat=s_J) i.i_region [aweight=wgt], a(year) robust
		summ ln_n_post_`var' if e(sample)==1 & s_treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest s_treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local wgt "$\checkmark$"	
		estadd local fe "$\checkmark$"		
		est sto `var'_4	
	
}

local titles "& \multicolumn{4}{c}{Primary schools} & \multicolumn{4}{c}{Other local public goods} \\"
local lines "\cmidrule(lr{0.5em}){2-5} \cmidrule(lr{0.5em}){6-9}"
local numbers "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
		
esttab pri_1 pri_2 pri_3 pri_4 /// 
	other_1 other_2 other_3 other_4 ///
	using "Table A15.tex",  ///
		replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
		nobaselevels keep(s_treat) stats(boot_p wgt fe DV_Mean DV_SD widstat N, labels(" " "\midrule Weights" "Region FE" "DV Mean" "DV SD" "FS F-statistic" "Observations") ///
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 0 2 2 1 0)) nolines		



********************************************************************************
*** HRDS instrumental variables regressions data *******************************
********************************************************************************

clear all

use "$root/Data/analysis_4.dta"

encode region, gen(i_region)

label variable J "\hspace{0.2cm} J"
label variable treat "\hspace{0.2cm} $\widehat{\text{Preferred}}$"

local distvar "ln_dist_pri ln_dist_sec ln_dist_disp ln_dist_hosp ln_dist_wp"
local qualvar "qual_z qual_pca"

foreach var in `distvar' `qualvar' {
	
	ivreghdfe `var' (treat=J) rural if index==1, a(year) cluster(constituency_year)
		summ `var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local fe "$\times$"
		estadd local wgt "$\times$"
		est sto `var'_1	
		
	ivreghdfe `var' (treat=J) rural if index==1 [aweight=wgt], a(year) cluster(constituency_year)
		summ `var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local fe "$\times$"
		estadd local wgt "$\checkmark$"
		est sto `var'_2

	ivreghdfe `var' (treat=J) rural i.i_region if index==1, a(year) cluster(constituency_year)
		summ `var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local fe "$\checkmark$"
		estadd local wgt "$\times$"
		est sto `var'_3
		
	ivreghdfe `var' (treat=J) rural i.i_region if index==1 [aweight=wgt], a(year) cluster(constituency_year)
		summ `var' if e(sample)==1 & treat==0
			estadd scalar DV_Mean = `r(mean)'
			estadd scalar DV_SD = `r(sd)'	
		boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
			estadd scalar boot_p = `r(p)'
		estadd local fe "$\checkmark$"		
		estadd local lag "$\checkmark$"		
		est sto `var'_4	
}

local numbers "& (1) & (2) & (3) & (4) \\ \midrule"

esttab ln_dist_pri_1 ln_dist_pri_2 ln_dist_pri_3 ln_dist_pri_4 using "Table A12.tex",  ///
	replace nonumber posthead("`numbers'") nomtitles b(2) not label refcat(treat "\textbf{A. Primary school}", nolabel) f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p DV_Mean DV_SD widstat N, labels(" " "\midrule DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 2 2 1 %9.0fc)) nolines
	
esttab ln_dist_sec_1 ln_dist_sec_2 ln_dist_sec_3 ln_dist_sec_4 using "Table A12.tex",  ///
	append nonumber nomtitles b(2) not label refcat(treat "\midrule \textbf{B. Secondary school}", nolabel) f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p DV_Mean DV_SD widstat N, labels(" " "\midrule DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 2 2 1 %9.0fc)) nolines
	
esttab ln_dist_disp_1 ln_dist_disp_2 ln_dist_disp_3 ln_dist_disp_4 using "Table A12.tex",  ///
	append nonumber nomtitles b(2) not label refcat(treat "\midrule \textbf{C. Dispensary}", nolabel) f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p DV_Mean DV_SD widstat N, labels(" " "\midrule DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 2 2 1 %9.0fc)) nolines
	
esttab ln_dist_hosp_1 ln_dist_hosp_2 ln_dist_hosp_3 ln_dist_hosp_4 using "Table A12.tex",  ///
	append nonumber nomtitles b(2) not label refcat(treat "\midrule \textbf{D. Health facility}", nolabel) f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p DV_Mean DV_SD widstat N, labels(" " "\midrule DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 2 2 1 %9.0fc)) nolines
	
esttab ln_dist_wp_1 ln_dist_wp_2 ln_dist_wp_3 ln_dist_wp_4 using "Table A12.tex",  ///
	append nonumber nomtitles b(2) not label refcat(treat "\midrule \textbf{E. Water point}", nolabel) f nogaps booktabs noobs compress ///
		nobaselevels keep(treat) stats(boot_p wgt fe DV_Mean DV_SD widstat N, labels(" " "\midrule Weights" "Region FE" "DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
		layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 0 2 2 1 %9.0fc)) nolines


local titles "& \multicolumn{4}{c}{ Z-score index } & \multicolumn{4}{c}{ First principal component } \\"
local lines "\cmidrule(lr{1em}){2-5} \cmidrule(lr{1em}){6-9} "
local numbers "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"

esttab qual_z_1 qual_z_2 qual_z_3 qual_z_4 qual_z_1 qual_z_2 qual_z_3 qual_z_4 using "Table A13.tex",  ///
	replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
	nobaselevels keep(treat) stats(boot_p wgt fe DV_Mean DV_SD widstat N, labels(" " "\midrule Weights" "Region FE" "DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
	layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 0 2 2 1 %9.0fc)) nolines nostar
	

local edvar "ed_ever lit_read_write"

foreach var in `edvar' {
	
	foreach max in 4 9 {
		
		ivreghdfe `var' (treat=J) i.rural i.yob i.male if yrs>=0 & yrs<=`max', a(year) cluster(constituency_year)
			summ `var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
				estadd scalar boot_p = `r(p)'
			estadd local fe "$\times$"
			estadd local wgt "$\times$"
			est sto `var'_`max'_1	
			
		ivreghdfe `var' (treat=J) i.rural i.yob i.male if yrs>=0 & yrs<=`max'  [aweight=wgt], a(year) cluster(constituency_year)
			summ `var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
				estadd scalar boot_p = `r(p)'
			estadd local fe "$\times$"
			estadd local wgt "$\checkmark$"
			est sto `var'_`max'_2

		ivreghdfe `var' (treat=J) i.rural i.yob i.male i.i_region if yrs>=0 & yrs<=`max', a(year) cluster(constituency_year)
			summ `var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
				estadd scalar boot_p = `r(p)'
			estadd local fe "$\checkmark$"
			estadd local wgt "$\times$"
			est sto `var'_`max'_3
			
		ivreghdfe `var' (treat=J) i.rural i.yob i.male i.i_region if yrs>=0 & yrs<=`max' [aweight=wgt], a(year) cluster(constituency_year)
			summ `var' if e(sample)==1 & treat==0
				estadd scalar DV_Mean = `r(mean)'
				estadd scalar DV_SD = `r(sd)'	
			boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
				estadd scalar boot_p = `r(p)'
			estadd local fe "$\checkmark$"	
			estadd local wgt "$\checkmark$"
			est sto `var'_`max'_4	
		
	}
}

local numbers "& (1) & (2) & (3) & (4) \\ \midrule"

local titles "& \multicolumn{4}{c}{ Ever went to school } & \multicolumn{4}{c}{ Can read and write } \\"
local lines "\cmidrule(lr{1em}){2-5} \cmidrule(lr{1em}){6-9} "
local numbers "\textbf{A. Baseline cohorts} & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"

esttab ed_ever_4_1 ed_ever_4_2 ed_ever_4_3 ed_ever_4_4 lit_read_write_4_1 lit_read_write_4_2 lit_read_write_4_3 lit_read_write_4_4 using "Table A14.tex",  ///
	replace nonumber posthead("`titles'" "`lines'" "`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
	nobaselevels keep(treat) stats(boot_p DV_Mean DV_SD widstat N, labels(" " "\midrule DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
	layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 2 2 1 %9.0fc)) nolines nostar

local numbers "\midrule \\ \textbf{B. Extended cohorts} & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule"
	
esttab ed_ever_9_1 ed_ever_9_2 ed_ever_9_3 ed_ever_9_4 lit_read_write_9_1 lit_read_write_9_2 lit_read_write_9_3 lit_read_write_9_4 using "Table A14.tex",  ///
	append nonumber posthead("`numbers'") nomtitles b(2) not label f nogaps booktabs noobs compress ///
	nobaselevels keep(treat) stats(boot_p wgt fe DV_Mean DV_SD widstat N, labels(" " "\midrule Weights" "Region FE" "DV Mean" "DV SD" "FS F-statistic" "Observations") /// 
	layout("\$\left[\text{@}\right]\$" "@" "@" "@") fmt(2 0 0 2 2 1 %9.0fc)) nolines nostar
	

********************************************************************************
*** Generating bootstrap IV confidence intervals for Figure A12 ****************
********************************************************************************

use "$root/Data/analysis_2.dta", clear

levelsof district_year, local(unique_d)
levelsof region_year, local(unique_r)

tempfile iv_district_pri
local counter = 1

foreach d of local unique_d {
    preserve
    drop if district_year == "`d'"
    ivreghdfe ln_n_post_pri (treat=J), a(year) robust
		local coef = _b[treat]	
	boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
		matrix ci_mat = r(CI)
		local ci_low = ci_mat[1,1]
		local ci_high = ci_mat[1,2]
	restore

    preserve
    clear
    set obs 1
    gen outcome = "Primary schools"
    gen spec = "District"
    gen excluded = "`d'"
	gen coef = `coef'
    gen ci_lower = `ci_low'
    gen ci_upper = `ci_high'
    
    if `counter' == 1 {
        save `iv_district_pri', replace
    }
    else {
        append using `iv_district_pri'
        save `iv_district_pri', replace
    }
    restore
    local counter = `counter' + 1
}	

use "$root/Data/analysis_2.dta", clear
tempfile iv_district_other
local counter = 1

foreach d of local unique_d {
    preserve
    drop if district_year == "`d'"
    ivreghdfe ln_n_post_other (treat=J), a(year) robust
		local coef = _b[treat]	
	boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
		matrix ci_mat = r(CI)
		local ci_low = ci_mat[1,1]
		local ci_high = ci_mat[1,2]
	restore

    preserve
    clear
    set obs 1
    gen outcome = "Other local public goods"
    gen spec = "District"
    gen excluded = "`d'"
	gen coef = `coef'
    gen ci_lower = `ci_low'
    gen ci_upper = `ci_high'
    
    if `counter' == 1 {
        save `iv_district_other', replace
    }
    else {
        append using `iv_district_other'
        save `iv_district_other', replace
    }
    restore
    local counter = `counter' + 1
}	

use "$root/Data/analysis_2.dta", clear
tempfile iv_region_pri
local counter = 1

foreach r of local unique_r {
    preserve
    drop if region_year == "`r'"
    ivreghdfe ln_n_post_pri (treat=J), a(year) robust
		local coef = _b[treat]	
	boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
		matrix ci_mat = r(CI)
		local ci_low = ci_mat[1,1]
		local ci_high = ci_mat[1,2]
	restore

    preserve
    clear
    set obs 1
    gen outcome = "Primary schools"
    gen spec = "Region"
    gen excluded = "`r'"
	gen coef = `coef'
    gen ci_lower = `ci_low'
    gen ci_upper = `ci_high'
    
    if `counter' == 1 {
        save `iv_region_pri', replace
    }
    else {
        append using `iv_region_pri'
        save `iv_region_pri', replace
    }
    restore
    local counter = `counter' + 1
}	

use "$root/Data/analysis_2.dta", clear
tempfile iv_region_other
local counter = 1

foreach r of local unique_r {
    preserve
    drop if region_year == "`r'"
    ivreghdfe ln_n_post_other (treat=J), a(year) robust
		local coef = _b[treat]	
	boottest treat, statistic(t) reps(5000) seed(94305) nograph ar
		matrix ci_mat = r(CI)
		local ci_low = ci_mat[1,1]
		local ci_high = ci_mat[1,2]
	restore

    preserve
    clear
    set obs 1
    gen outcome = "Other local public goods"
    gen spec = "Region"
    gen excluded = "`r'"
	gen coef = `coef'
    gen ci_lower = `ci_low'
    gen ci_upper = `ci_high'
    
    if `counter' == 1 {
        save `iv_region_other', replace
    }
    else {
        append using `iv_region_other'
        save `iv_region_other', replace
    }
    restore
    local counter = `counter' + 1
}	

use `iv_district_pri', clear
append using `iv_district_other'
append using `iv_region_pri'
append using `iv_region_other'
export delimited "$root/Data/iv_jackknife.csv", replace	
